//
//  HWGameCircleHeader.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/12/30.
//

import UIKit

class HWGameCircleHeader: UICollectionReusableView {
        
    var bgView = c_make_ex(UIView.self)
        .bgColor(.white).corner(12*widthScale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner]).end()
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var nameLabelBg:UIView!
    
    var gradientLabel: UILabel!
    var gradient: CAGradientLayer!
    
    var nameLabel:GranibleLabel!
    var timeLabel:UILabel!
    var contentLabel:UILabel!
    var medalImageView:UIImageView!
    var vipImageView:UIImageView!
    var tagBgView:UIView!
    var tagLabel:UILabel!
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var jumpToUserDetail:((_:String)->())?
    var model:HWGameCircleModel?{
        didSet {
            nameLabel.removeFromSuperview()
            nameLabel = GranibleLabel()
            nameLabel.font = .boldSystemFont(ofSize: 14*widthScale)
            nameLabel.isUserInteractionEnabled = true
            bgView.addSubview(nameLabel)
            
            
            headImageView.sd_setImage(with: URL(string: model?.face ?? ""), completed: nil)
            nameLabel.text = model?.pet_name
            contentLabel.text = model?.content
            timeLabel.text = model?.time
            medalImageView.sd_setImage(with: URL(string: model?.medal ?? ""), completed: nil)
            vipImageView.sd_setImage(with: URL(string: model?.huiyuan_img ?? ""), completed: nil)
            tagLabel.text = model?.note
            tagLabel.isHidden = false
            tagBgView.isHidden = false
            if model?.note_type == 1 {
                tagBgView.backgroundColor = .colorWithHexString(hexString: "#FFECEC")
                tagLabel.textColor = .colorWithHexString(hexString: "#FF5D5D")
            }else if model?.note_type == 2 {
                tagLabel.layoutIfNeeded()
                tagLabel.textColor = .colorWithHexString(hexString: "#6A3400")
                UIColor.gradientColor(view: tagBgView, colors: [
                    UIColor.colorWithAlphaHexString(hexString: "#FFEAB9", alpha: 1).cgColor,
                    UIColor.colorWithAlphaHexString(hexString: "#E49D6A", alpha: 1).cgColor,],start: CGPoint(x: 0, y: 0),end: CGPoint(x: 1, y: 0))
            }else{
                tagLabel.isHidden = true
                tagBgView.isHidden = true
            }
            
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
            
            
            
//            if model?.head_frame.count ?? 0 > 0{
//                headImageView.snp.remakeConstraints { make in
//                    make.left.equalTo(30*widthScale)
//                    make.top.equalTo(20*widthScale)
//                    make.width.height.equalTo(50*widthScale)
//                }
//                nameLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(headFrameImageView.snp.right).offset(6*widthScale)
//                    make.top.equalTo(headImageView).offset(8*widthScale)
//                }
//
//
//            }else{
//                headImageView.snp.remakeConstraints { make in
//                    make.left.equalTo(20*widthScale)
//                    make.top.equalTo(10*widthScale)
//                    make.width.height.equalTo(50*widthScale)
//                }
//                nameLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(headImageView.snp.right).offset(6*widthScale)
//                    make.top.equalTo(headImageView).offset(8*widthScale)
//                }
//
//            }
            nameLabel.snp.makeConstraints { make in
                make.left.equalTo(67*widthScale)
                make.top.equalTo(headImageView).offset(5*widthScale)
            }
            
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#444444"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#444444")]
            }
            medalImageView.snp.remakeConstraints { make in
                make.left.equalTo(nameLabel.snp.right).offset(6*widthScale)
                make.centerY.equalTo(nameLabel)
                make.width.height.equalTo(16*widthScale)
            }
            
            tagBgView.snp.remakeConstraints { make in
                make.left.equalTo(nameLabel)
                make.height.equalTo(12*widthScale)
                make.width.equalTo(38*widthScale)
                make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWGameCircleHeader {
    private func initUI() -> Void {
        backgroundColor = .clear
//        bgView = UIView()
//        bgView.backgroundColor = UIColor.white
        addSubview(bgView)
        
        headImageView = UIImageView()
        headImageView.backgroundColor = UIColor.clear
        headImageView.layer.cornerRadius = 20*widthScale
        headImageView.layer.masksToBounds = true
        headImageView.isUserInteractionEnabled = true
        headImageView.contentMode = .scaleAspectFill
        bgView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        bgView.addSubview(headFrameImageView)
        
        nameLabel = GranibleLabel()
        nameLabel.text = ""
        nameLabel.font = .boldSystemFont(ofSize: 14*widthScale)
        nameLabel.isUserInteractionEnabled = true
        bgView.addSubview(nameLabel)
        
        timeLabel = HWLabel.createLabel(title: "-", color: "#999999", font: 10*widthScale)
        bgView.addSubview(timeLabel)
       
        
        medalImageView = UIImageView.init()
        medalImageView.backgroundColor = .clear
        bgView.addSubview(medalImageView)
        
        vipImageView = UIImageView.init()
        vipImageView.backgroundColor = .clear
        bgView.addSubview(vipImageView)
        
        tagBgView = UIView()
        tagBgView.layer.cornerRadius = 2*widthScale
        tagBgView.layer.masksToBounds = true
        tagBgView.isHidden = true
        tagBgView.frame = CGRect(x: 0, y: 0, width: 38*widthScale, height: 12*widthScale)
        bgView.addSubview(tagBgView)
        
        tagLabel = HWLabel.createBoldLabel(title: "", color: "#FF5D5D", font: 8*widthScale)
        tagLabel.textAlignment = .center
        tagLabel.backgroundColor = .clear
        bgView.addSubview(tagLabel)
        
        contentLabel = HWLabel.createLabel(title: "-", color: "#333333", font: 13*widthScale)
        bgView.addSubview(contentLabel)
        
        
        let tapBg = UITapGestureRecognizer.init(target: self, action: #selector(tapBg))
        bgView.addGestureRecognizer(tapBg)
        
        let tapHeadImage = UITapGestureRecognizer.init(target: self, action: #selector(self.tapHeadImage))
        headImageView.addGestureRecognizer(tapHeadImage)
        
    }
    
    private func initFrame() -> Void {
        bgView.snp.makeConstraints { make in
            make.top.left.equalTo(12*widthScale)
            make.right.equalTo(-12*widthScale)
            make.bottom.equalTo(0)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(15*widthScale)
            make.top.equalTo(10*widthScale)
            make.width.height.equalTo(40*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.height.width.equalTo(54*widthScale)
        }
        
//        nameLabelBg.snp.makeConstraints { make in
//            make.left.equalTo(headImageView.snp.right).offset(6*widthScale)
//            make.top.equalTo(headImageView).offset(8*widthScale)
//        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(67*widthScale)
            make.top.equalTo(headImageView).offset(5*widthScale)
        }
//        nameLabel.topAnchor.constraint(equalTo: headImageView.bottomAnchor, constant: 8).isActive = true
//        nameLabel.leadingAnchor.constraint(equalTo: headImageView.trailingAnchor, constant: 6).isActive = true
//        nameLabel.trailingAnchor.constraint(equalTo: bgView.trailingAnchor, constant: -20).isActive = true
//        gradientLabel.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
        medalImageView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(nameLabel)
            make.width.height.equalTo(16*widthScale)
        }
        
        vipImageView.snp.makeConstraints { make in
            make.left.equalTo(medalImageView.snp.right).offset(6*widthScale)
            make.centerY.equalTo(medalImageView)
//            make.height.equalTo(16*widthScale)
//            make.width.equalTo(38*widthScale)
            make.width.height.equalTo(16*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalTo(67*widthScale)
            make.bottom.equalTo(headImageView).offset(-3*widthScale)
        }
        
        tagBgView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.height.equalTo(12*widthScale)
            make.width.equalTo(38*widthScale)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
        }
        
        tagLabel.snp.makeConstraints { make in
//            make.left.equalTo(nameLabel)
//            make.height.equalTo(12*widthScale)
//            make.width.equalTo(38*widthScale)
//            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            make.left.right.top.bottom.equalTo(tagBgView)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(tagBgView)
            make.top.equalTo(headImageView.snp.bottom).offset(8*widthScale)
            make.right.equalTo(-15*widthScale)
//            make.bottom.equalTo(0)
        }
    }
}
extension HWGameCircleHeader{
    
    @objc func tapBg() -> Void {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
    @objc func tapHeadImage() -> Void {
        jumpToUserDetail!("")
    }
}
